<template>
  <!-- 小智同学需要单独开启服务器 在教学资料文件夹中 -->
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar fixed left-arrow title="小智同学" @click-left="$router.back()" />

    <!-- 聊天主体区域 -->
    <div ref="chatList" class="chat-list">
      <!-- 左侧是机器人小智 -->
      <div v-for="(item,index) in list" :key="index" class="chat-item" :class="item.name === 'xz' ? 'left' : 'right'">
        <!-- 小智 -->
        <!-- template 只是一个包裹性质的容器, 不会渲染在html结果中 -->
        <template v-if="item.name === 'xz'">
          <van-image fit="cover" round src="https://img1.baidu.com/it/u=132932751,784541725&fm=26&fmt=auto&gp=0.jpg" />
          <div class="chat-pao">{{ item.msg }}</div>
        </template>
        <!-- 我的 -->
        <template v-else>
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="userInfo.photo" />
        </template>
      </div>

      <!-- 右侧是当前用户 -->
      <!-- <div class="chat-item right" /> -->
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span slot="button" style="font-size:12px;color:#999" @click="send">提交</span>
      </van-field>
    </div>
  </div>
</template>

<script>
import { reqGetUserProfile } from '@/api/user'
import io from 'socket.io-client'
export default {
  name: 'UserChat',
  data() {
    return {
      word: '',
      userInfo: {},
      list: [ // 对话记录
        // { name: 'xz', msg: '你好，我是无所不知的小智同学！', timestamp: Date.now() },
        // { name: 'me', msg: '小智同学, 请问今天下雨吗？', timestamp: Date.now() },
        // { name: 'me', msg: '快帮我查查', timestamp: Date.now() },
        // { name: 'xz', msg: '今天不下雨', timestamp: Date.now() },
        // { name: 'me', msg: 'xxx', timestamp: Date.now() }
      ],
      socket: null
    }
  },
  async created() {
    const { data: { data }} = await reqGetUserProfile()
    this.userInfo = data
    // 2.调用建立连接的方法
    this.initSocket()
  },
  // 销毁组件断开链接
  destroyed() {
    console.log('销毁了')
    this.socket.close()
  },
  methods: {
    initSocket() {
      // 1.建立连接
      this.socket = io('http://localhost:8888')
      // 3. 监听连接状态 提示用户
      this.socket.on('connect', () => {
        this.$toast.success('会话接入成功')
      })
      // 4.接收服务器消息
      // this.socket.on('消息类型', (msg)=>{})
      this.socket.on('message', (msg) => {
        // console.log(msg)
        // 5.接收到消息, 追加给list数组 添加name字段
        this.list.push({ ...msg, name: 'xz' })
        // 处理滚动条
        // 6. 每次数据更新后 重新设置 chatlist元素的scrollTop值scrollTop是可读可写的
        // scrollHeight 如果元素没有滚动条,获取到的就是元素的高度
        // scrollHeight 如果元素有了滚动条,获取到的是元素的高度+滚出的高度
        this.$nextTick(() => {
          this.$refs.chatList.scrollTop = this.$refs.chatList.scrollHeight
        })
      })
    },
    // 发消息给服务端
    send() {
      // 1. 发送消息给服务端
      // this.socket.emit('消息类型', 消息)
      this.socket.emit('message', { timestamp: Date.now(), msg: this.word })
      // 2. 将我的信息追加到list数组中
      this.list.push({
        name: 'me',
        timestamp: Date.now(),
        msg: this.word
      })
      // 3. 清空输入框
      this.word = ''
      this.$nextTick(() => {
        this.$refs.chatList.scrollTop = this.$refs.chatList.scrollHeight
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background:#fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item{
      padding: 10px;
      .van-image{
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao{
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before{
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top:0.5px solid #c2d9ea;
          border-right:0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right{
  text-align: right;
  .chat-pao{
    margin-left: 0;
    margin-right: 15px;
    &::before{
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left{
  text-align: left;
  .chat-pao{
    margin-left: 15px;
    margin-right: 0;
    &::before{
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
